import { MenuDataItem } from "@ant-design/pro-components"
import { CrownOutlined } from "@ant-design/icons"
import AccessEnum from "@/access/assess-enum"

export const menus: MenuDataItem[] = [
  { path: "/", name: "主页" },
  { path: "/banks", name: "题库" },
  { path: "/questions", name: "题目" },
  { path: "https://mianshiya.com", name: "面试鸭" },
  {
    path: "/admin",
    name: "管理",
    icon: <CrownOutlined />,
    access: AccessEnum.ADMIN,
    children: [
      { path: "/admin/user", name: "用户管理", access: AccessEnum.ADMIN },
      { path: "/admin/bank", name: "题库管理", access: AccessEnum.ADMIN },
      { path: "/admin/question", name: "题目管理", access: AccessEnum.ADMIN }
    ]
  }
]

/**
 * 根据路径查找所有菜单
 *
 * @param path 路径
 */
export const findAllMenuByPath = (path: string) => {
  return findMenuByPath(menus, path)
}

/**
 * 根据路径查找菜单
 *
 * @param menus 菜单列表
 * @param path 路径
 */
const findMenuByPath = (menus: MenuDataItem[], path: string): MenuDataItem | null => {
  for (const menu of menus) {
    if (menu.path === path) {
      return menu
    }

    if (menu.children) {
      const findMenu = findMenuByPath(menu.children, path)
      if (findMenu) {
        return findMenu
      }
    }
  }

  return null
}
